package fr.esiea.es_inf5042.gui;

import fr.esiea.es_inf5042.MainActivity;
import fr.esiea.es_inf5042.R;
import fr.esiea.es_inf5042.business.*;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class BargainCreatorDialog extends Dialog{
	
	private Context context;
	private MainActivity main;
	
	private Category categoryBargain = null;
	private String titleBargain = null;
	private String descriptionBargain = null;
	private float markBargain = -1;
	
	private EditText editTextTitle;
	private EditText editTextDescription;
	private EditText editTextMark;
	
	private Bargain bargain;
	private User theCreator;
	private Location locationBargain;
	
	public BargainCreatorDialog(Context context) {
		super(context);
		this.context = context;
		this.main = (MainActivity) context;
		this.bargain = null;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bargain_creator_dialog);
		setTitle(R.string.title_bargain_creator_dialog);
		setCancelable(true);
        
		// set up Title
        editTextTitle = (EditText) findViewById(R.id.EditTextTitle);

        // set up Description
        editTextDescription = (EditText) findViewById(R.id.EditTextDescription);
        
        // set up Categories
        ArrayList<String> listCategories = new ArrayList<String>();
        for (Category cat : Category.values())
        	listCategories.add(cat.toString());
        Spinner spin = (Spinner) findViewById(R.id.spinner);
		spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
				// get the category chosen
				categoryBargain = Category.valueOf(parent.getItemAtPosition(position).toString().toUpperCase());
			}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub	
			}
		});
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,listCategories);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adapter);
		
		// set up Mark
        editTextMark = (EditText) findViewById(R.id.EditTextMark);
        
        // set up Button OK
        Button buttonOk = (Button) findViewById(R.id.ButtonOk);
		buttonOk.setOnClickListener(new buttonOkListener());
        
        // set up Button CANCEL
        Button  buttonCancel = (Button) findViewById(R.id.ButtonCancel);
        buttonCancel.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View  v) {
                dismiss();
            }
        });		
	}


	
	public void setTheCreator(User theCreator) {
		this.theCreator = theCreator;
	}



	public void setLocationBargain(Location locationBargain) {
		this.locationBargain = locationBargain;
	}



	public Category getCategoryBargain() {
		return categoryBargain;
	}



	public void setCategoryBargain(Category categoryBargain) {
		this.categoryBargain = categoryBargain;
	}



	public String getTitleBargain() {
		return titleBargain;
	}



	public void setTitleBargain(String titleBargain) {
		this.titleBargain = titleBargain;
	}



	public String getDescriptionBargain() {
		return descriptionBargain;
	}



	public void setDescriptionBargain(String descriptionBargain) {
		this.descriptionBargain = descriptionBargain;
	}


	public float getMarkBargain() {
		return markBargain;
	}



	public void setMarkBargain(float markBargain) {
		this.markBargain = markBargain;
	}

	private void displayToast(String msg) {
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}

	
	private class buttonOkListener implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			String tmptitleBargain = editTextTitle.getText().toString();
			String tmpdescriptionBargain = editTextDescription.getText().toString();
			String mark = editTextMark.getText().toString();
			
			if (!tmptitleBargain.equals("") && !tmpdescriptionBargain.equals("") && categoryBargain != null && !mark.equals(""))
			{
				float tmpmarkBargain = Float.valueOf(mark.trim()).floatValue();
				// Check if the mark is between 0 and 5
				if (tmpmarkBargain <= 5 && tmpmarkBargain >=0)
				{
					try {
						titleBargain = tmptitleBargain;
						descriptionBargain = tmpdescriptionBargain;
						markBargain = tmpmarkBargain;
						bargain = new Bargain(0, theCreator, titleBargain, descriptionBargain, null, categoryBargain, markBargain, locationBargain);
						bargain = (Bargain) main.createEntity(bargain);
						displayToast("Bargain created");
						main.createBargainOverlay(bargain);
						dismiss();
					} catch (Exception e) {
						displayToast("ERROR - Bargain not created");
						dismiss();
					}
				}
				else {
					displayToast("The mark has to be between 0 and 5 !");
				}
			}
			else {
				displayToast("All fiels have to be filled !");
			}
			
		}
		
	}
}
